package tijava.container;

import java.util.Collections;
import java.util.Set;
import java.util.TreeSet;

import tijava.container.type.ComparableType;

/**
 * 使用TreeSet的类必须实现Comparable接口,但不必重写equals和hashCode方法，与TreeSet的内部实现有关
 * 
 * @see tijava.container.type.ComparableType
 * @author nathan
 * 
 */
public class TreeSetDemo {
	public static void main(String[] args) {
		Set<ComparableType> q = new TreeSet<ComparableType>();
		q.add(new ComparableType('d', 3, null));
		q.add(new ComparableType('d', 4, null));
		q.add(new ComparableType());
		q.add(new ComparableType());
		q.add(new ComparableType());
		q.add(new ComparableType());
		q.add(new ComparableType());

		System.out.println("the set's size is:" + q.size());
		for (ComparableType i : q) {
			System.out.println(i);
		}
		if (q.contains(new ComparableType('d', 3, null))) {
			System.out.println("e found in set");
		}
		if (q.remove(new ComparableType('d', 3, null))) {
			System.out.println("e removed in set");
		}
		System.out.println("the set's size is:" + q.size());

		// reverseOrder
		Set<ComparableType> q1 = new TreeSet<ComparableType>(
				Collections.reverseOrder());
		q1.add(new ComparableType('d', 3, null));
		q1.add(new ComparableType('d', 4, null));
		q1.add(new ComparableType());
		q1.add(new ComparableType());
		q1.add(new ComparableType());
		q1.add(new ComparableType());
		q1.add(new ComparableType());
		System.out.println("the set's size is:" + q1.size());
		for (ComparableType i : q1) {
			System.out.println(i);
		}

	}

}
